package binarytree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * GetTheMinSumOfTheTreePath 寻找一颗满二叉树中路径和最小的值
 *
 * @author <a href="zhaoyingling12@163.com">Zhao Simon</a>
 * @description: 给定一颗满二叉树，获得最小的路径和
 * @date 2021/5/7
 **/
public class GetTheMinSumOfTheTreePath {

    public int findTheMinSumOfTheTree(TreeNode root) {
        if (root == null) {
            return -1;
        }
        List<Integer> ans = new ArrayList<>();
        theSumOfTheTreePath(root, ans, root.val);
        int min = Integer.MAX_VALUE;
        for (int tmp : ans) {
            min = Math.min(min, tmp);
        }
        return min;
    }

    private void theSumOfTheTreePath(TreeNode root, List<Integer> list, int sum) {
        if (root.left == null && root.right == null) {
            list.add(sum);
        }
        if (root.left != null) {
            theSumOfTheTreePath(root.left, list, sum + root.left.val);
        }
        if (root.right != null) {
            theSumOfTheTreePath(root.right, list, sum + (root.right.val));
        }
    }
}
